// BibleSheet.cpp : implementation file
//

#include "stdafx.h"
#include "SwordBible.h"
#include "BibleSheet.h"
#include ".\biblesheet.h"
#include "SwordBibleView.h"


// CBibleSheet
CScripRef * CBibleSheet::pDlg = NULL;
HCURSOR CBibleSheet::m_hHandCursor; 


IMPLEMENT_DYNAMIC(CBibleSheet, CPropertySheet)
CBibleSheet::CBibleSheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{

    title = nIDCaption;

	pDlg = NULL;

	pMgr = NULL;
	pMgrPlain = NULL;

	pMgr = new sword::SWMgr(0, 0, TRUE, new sword::MarkupFilterMgr(sword::FMT_RTF, sword::ENC_RTF));
	pMgrPlain = new sword::SWMgr(0, 0, TRUE, new sword::MarkupFilterMgr(sword::FMT_PLAIN, sword::ENC_RTF));

	OnToggleTags(0,0); //turn on/off strong's, morphological tags
	pMgr->setGlobalOption("Greek Accents","Off");
	pMgr->setGlobalOption("Hebrew Cantillation","Off");
	pMgr->setGlobalOption("Hebrew Vowel Points","Off");

	pMgrPlain->setGlobalOption("Cross-references","Off");
	pMgrPlain->setGlobalOption("Greek Accents","Off");
	pMgrPlain->setGlobalOption("Hebrew Cantillation","Off");
	pMgrPlain->setGlobalOption("Hebrew Vowel Points","Off");
	pMgrPlain->setGlobalOption("Headings","On");

	// finds modules installed on the system.
	sword::SWModule *pModule = NULL;  //pointer to a specific module
	sword::ModMap::iterator it;
	no_mods_page = NULL;
	  
	CString version = "";
	int added = 0;

	
	// Add pages to the top sheet
	if(nIDCaption == AFX_MAINBIBLE)
	{
      

	for(int i = 0; i < nBibles; i++) BiblePages[i] = NULL;
	parallelPage = NULL;

	for (it = pMgr->Modules.begin(); it != pMgr->Modules.end(); it++) {
		
		pModule = it->second;
		
		if (!strcmp(pModule->Type(), "Biblical Texts")) {
	
		BiblePages[added] = new CBiblePage();
		AddPage(BiblePages[added]);
		BiblePages[added]->version = pModule->Name();
		BiblePages[added]->m_psp.dwFlags |= PSP_USETITLE;
		BiblePages[added]->m_psp.pszTitle = _T(  pModule->Name() );
		BiblePages[added]->pModule = pModule;
		BiblePages[added]->pModulePlain = pMgrPlain->getModule(pModule->Name()); // passed on to the search function
		
		CSwordBibleDoc::pModulePlain[added] = BiblePages[added]->pModulePlain;
		added++;
		}
	}   // end of for loop

	   CSwordBibleDoc::numBiblesAvailable = added;

	   // add the parallel page
	   parallelPage = new CParallelBible();
	   AddPage(parallelPage);  
       parallelPage->m_psp.dwFlags |= PSP_USETITLE;
	   parallelPage->m_psp.pszTitle = _T("Parallel");
       added++;
	


	

	// add the pointers to the Greek, Hebrew and KJV modules to the bible page
	CBiblePage::pGreek = pMgrPlain->getModule("StrongsGreek");
	CBiblePage::pHebrew = pMgrPlain->getModule("StrongsHebrew");
	CBiblePage::pThayer = pMgrPlain->getModule("Thayer");
	CBiblePage::pRobinson = pMgrPlain->getModule("Robinson");
    SetXrefActiveModule();//  Set the default module for displaying cross-references 
	


	  AfxGetMainWnd()->SendMessageToDescendants(WM_TREE_SETSEL,0,0,TRUE);
	CSwordBibleView* pView = (CSwordBibleView*) ((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
	CBiblePage::pDoc = (CSwordBibleDoc*)pView->GetDocument();
	CParallelBible::pDoc = (CSwordBibleDoc*)pView->GetDocument();

	

	

	} // done adding pages for the bibles


   
	 
   



	// Add pages to the bottom sheet
	if(nIDCaption == AFX_BOTTOM)
	{

	// Add Page for the notes
	notes_page = NULL;
	notes_page = new CNotesPage(); 
	AddPage(notes_page);
	notes_page->m_psp.dwFlags |= PSP_USETITLE;
	notes_page->m_psp.pszTitle = _T("Notes");

		

		for(int i = 0; i < nBottom; i++) BottomPages[i] = NULL;

		added = 1; // notes page already added
		
		for (it = pMgr->Modules.begin(); it != pMgr->Modules.end(); it++) {
		
		
		pModule = it->second;
		
		if (   !strcmp(pModule->Type(), "Commentaries")) {
	
		BottomPages[added] = new CBottomPage();
		AddPage(BottomPages[added]);
		BottomPages[added]->version = pModule->Name();
		BottomPages[added]->m_psp.dwFlags |= PSP_USETITLE;
		BottomPages[added]->m_psp.pszTitle = _T(  pModule->Name() );
		BottomPages[added]->pModule = pModule;
		added++;
		
	
		}
	}   // end of for loop 
	} // done adding pages for the bottom modules

   /************************ ADD PAGES FOR BOTTOM LEFT MODULES ***************************************/
	////////////////////////////////////////////////////////////////////////////////////////////////////
	CString isDevotion = "DailyDevotion";
	CString feature = "";
	
	if(nIDCaption == AFX_BOTTOMLEFT)
	{
	 for(int i = 0; i < nBottomLeft; i++) BottomLeftPages[i] = NULL;
	 added = 0;
	 
	for (it = pMgr->Modules.begin(); it != pMgr->Modules.end(); it++) {
		
	
	pModule = it->second;
	feature = pModule->getConfigEntry("Feature");
		
	 if ( (!strcmp(pModule->Type(), "Lexicons / Dictionaries")) && 
		 ( isDevotion.CompareNoCase(feature) != 0 ) ) {		
				  

		BottomLeftPages[added] = new CBottomLeft();
	    AddPage(BottomLeftPages[added]);
	    BottomLeftPages[added]->version = pModule->Name();
		BottomLeftPages[added]->m_psp.dwFlags |= PSP_USETITLE;
	    BottomLeftPages[added]->m_psp.pszTitle = _T(  pModule->Name()  );
	    if( pModule->Name() == "StrongsGreek") BottomLeftPages[added]->m_psp.pszTitle  = "Greek";          //manual override of title
	    else if( pModule->Name() == "StrongsHebrew") BottomLeftPages[added]->m_psp.pszTitle  = "Hebrew";   // manual override of title
	    BottomLeftPages[added]->pModule = pModule;	
				added++;
		
	
		}

	 } // end of the for loop


	if(added == 0) // no modules added
	{ no_mods_page = new CNoModsPage(); AddPage(no_mods_page); }


	} // done adding pages for the bottom left modules
	
	
	/************************ ADD PAGES FOR DEVOTIONAL MODULES ***************************************/
	////////////////////////////////////////////////////////////////////////////////////////////////////

    

	if(nIDCaption == AFX_DEVOTIONAL)
	{
		for(int i = 0; i < nDevotional; i++) DevotionalPages[i] = NULL;
		added = 0;
	    

	for (it = pMgr->Modules.begin(); it != pMgr->Modules.end(); it++) {
		
	pModule = it->second;
	feature = pModule->getConfigEntry("Feature");
  
		
	if ( isDevotion.CompareNoCase(feature) == 0 )  {		
				  

		DevotionalPages[added] = new CDevotionalPage();
	    AddPage(DevotionalPages[added]);
	    DevotionalPages[added]->version = pModule->Name();
		DevotionalPages[added]->m_psp.dwFlags |= PSP_USETITLE;
	    DevotionalPages[added]->m_psp.pszTitle = _T(  pModule->Name()  );
	    DevotionalPages[added]->pModule = pModule;	
		added++;

	}
	} // end of for loop

		
	if(added == 0) // no modules added
		{ no_mods_page = new CNoModsPage();  AddPage(no_mods_page); }


	} // done adding pages for the devotionals





	/************************ ADD PAGES FOR DAILY BIBLE ***************************************/
	////////////////////////////////////////////////////////////////////////////////////////////////////

    

	if(nIDCaption == AFX_DAILYBIBLE)
	{
		DBPage = new CDailyBiblePage;
		AddPage(DBPage);
	    DBPage->m_psp.dwFlags |= PSP_USETITLE;
	    DBPage->m_psp.pszTitle = _T(  "Daily Bible" );

		/*RPPage = new CReadingPlanPage;
		AddPage(RPPage);
	    RPPage->m_psp.dwFlags |= PSP_USETITLE;
	    RPPage->m_psp.pszTitle = _T(  "Reading Plan" );    // Reading Plan feature will be in the next edition of SwordBible
	*/
	
	

	} // done adding pages for the Daily Bible












}


/////////  Set the default module for displaying cross-references ////////////////////////////////////
void CBibleSheet::SetXrefActiveModule()
{

	CString name = AfxGetApp()->GetProfileString("Tags", "XrefModule", ""); // name Xref module name from registry
	CSwordBibleDoc::pXrefModuleActive = pMgrPlain->getModule(name);    // static module pointer utilized for scripture references
	
	if(CSwordBibleDoc::pXrefModuleActive == NULL) 
			CSwordBibleDoc::pXrefModuleActive = CSwordBibleDoc::pModulePlain[0];
	

		// send message to mainframe to load the bible versions that can be used for cross-references
	AfxGetMainWnd()->SendMessage(WM_LOAD_XREF_BIBLEVERSIONS,0,0);


}


CBibleSheet::CBibleSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
	
}

CBibleSheet::~CBibleSheet()
{
	int i = 0;

    // if the sheet instance has no modules, a no mods page would have been created
	if(no_mods_page) delete no_mods_page;


	// if this property sheet instance is for the main bible pages, delete all pages it created
	if(title == AFX_MAINBIBLE) 
	{
		for (i = 0; i < nBibles; i++) 
			if( BiblePages[i] )
				delete BiblePages[i];

		if(parallelPage) parallelPage;
	}


	// if this property sheet instance is for the bottom pages, delete all bottom pages including notes property page
	else if (title == AFX_BOTTOM)
	{
        if( notes_page) delete  notes_page;

		for(i = 0; i < nBottom; i++)
			if(BottomPages[i])
				delete BottomPages[i];
	}


	// if this property sheet instance is for the bottom left pages, delete all pages it created
	else if(title == AFX_BOTTOMLEFT)
	{
		for(i = 0; i < nBottomLeft; i++)
			if(BottomLeftPages[i])
				delete BottomLeftPages[i];
	}




    // if this property sheet instance is for the devotional property pages, delete all pages it created
	else if(title == AFX_DEVOTIONAL)
	{
		for(i = 0; i < nDevotional; i++)
			if(DevotionalPages[i])
				delete DevotionalPages[i];
	
	}


	   // if this property sheet instance is for the daily bible property pages, delete all pages it created
	else if(title == AFX_DAILYBIBLE)
	{
		if(DBPage) delete DBPage;
	//	if(RPPage) delete RPPage;
	
	}

		if (pMgr)
	{ delete pMgr ; pMgr  = NULL; }

		
		if (pMgrPlain)
	{ delete pMgrPlain ; pMgrPlain  = NULL; }


	
	
}


LONG CBibleSheet::OnResizePage(UINT, LONG)
 {
     // resize the page using m_PageRect which was set in ScaleSheet
     CPropertyPage* pPage = GetActivePage ();
   //  ASSERT (pPage);
     pPage->MoveWindow (&m_PageRect);

     return 0;
 }

void CBibleSheet::ScaleSheet(int Height, int Width)
{


	// resize the sheet
	 RECT rc;
     GetWindowRect (&rc);
   	 ScreenToClient (&rc);
     rc.right += Width ;
     rc.bottom += Height;
     MoveWindow (&rc);

	 // resize the CTabCtrl

     CTabCtrl* pTab = GetTabControl ();
     ASSERT (pTab);
     pTab->GetWindowRect (&rc);
     ScreenToClient (&rc);
     rc.right += Width ;
     rc.bottom += Height;
     pTab->MoveWindow (&rc);


	 // resize the page

     CPropertyPage* pPage = GetActivePage ();
    // ASSERT (pPage);

     // store page size in m_PageRect
     pPage->GetWindowRect (&m_PageRect);
     ScreenToClient (&m_PageRect);

     m_PageRect.right += Width;
     m_PageRect.bottom += Height;
     pPage->MoveWindow (&m_PageRect);	
	 
	 
	
}


BEGIN_MESSAGE_MAP(CBibleSheet, CPropertySheet)

// manually added message map for resizing page
ON_MESSAGE (WM_RESIZEPAGE, OnResizePage)
ON_MESSAGE (WM_TOGGLE_TAGS, OnToggleTags)



ON_WM_DESTROY()
END_MESSAGE_MAP()


// CBibleSheet message handlers

BOOL CBibleSheet::OnInitDialog()
{
	BOOL bResult = CPropertySheet::OnInitDialog();

	// this will ensure that the left/right arrows show up if all tabs cannot fit in the view
	HWND hWnd = (HWND)::GetDlgItem(m_hWnd, AFX_IDC_TAB_CONTROL);
	if(::IsWindow(hWnd))
    CWnd::ModifyStyle(hWnd, TCS_MULTILINE, TCS_SINGLELINE, 0);

	// TODO:  Add your specialized code here
	// initialize m_pageRect and store page size in m_PageRect
		
	
	 CPropertyPage* pPage = GetActivePage ();
   //  ASSERT (pPage);
     pPage->GetWindowRect (&m_PageRect);
	 ScreenToClient(&m_PageRect);

  SetActivePage(0);

    m_hHandCursor = AfxGetApp()->LoadCursor(IDC_CURSOR_HAND);

	

	return bResult;
}

BOOL CBibleSheet::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: Add your specialized code here and/or call the base class
	NMHDR* pnmh = (LPNMHDR) lParam;


	// the sheet resizes the page whenever it is activated
     // so we need to resize it to what we want
     if (TCN_SELCHANGE == pnmh->code)
         // user-defined message needs to be posted because page must
         // be resized after TCN_SELCHANGE has been processed
         PostMessage (WM_RESIZEPAGE);


	return CPropertySheet::OnNotify(wParam, lParam, pResult);
}

bool CBibleSheet::GetAppDirectory( CString& strAppDir )
{
   bool bRet        = false;
   DWORD dwSize     = _MAX_PATH + 1;
   LPTSTR pszAppDir = new TCHAR[dwSize];

   if( ::GetModuleFileName( NULL, pszAppDir, dwSize ) )
   {
      strAppDir = pszAppDir;
      int nPos = 0; 
	  nPos = strAppDir.ReverseFind(_T('\\'));
      if( -1 != nPos )
      {
         strAppDir = strAppDir.Left( nPos );
         bRet = true;
      }
   }
   delete []pszAppDir;

   return bRet;

}

BOOL CBibleSheet::PreTranslateMessage(MSG* pMsg)
{
	return CPropertySheet::PreTranslateMessage(pMsg);
}

LRESULT CBibleSheet::OnToggleTags(WPARAM, LPARAM)
{
	if(CSwordBibleDoc::lemma_tags) pMgr->setGlobalOption("Strong's Numbers", "On");
	else pMgr->setGlobalOption("Strong's Numbers", "Off");

	if(CSwordBibleDoc::morph_tags) pMgr->setGlobalOption("Morphological Tags","On");
	else pMgr->setGlobalOption("Morphological Tags","Off");

	if(CSwordBibleDoc::fn_tags) pMgr->setGlobalOption("Footnotes","On");	
	else pMgr->setGlobalOption("Footnotes","Off");

	if(CSwordBibleDoc::crossref_tags) pMgr->setGlobalOption("Cross-references","On");	
	else pMgr->setGlobalOption("Cross-references","Off");

	if(CSwordBibleDoc::heading_tags) pMgr->setGlobalOption("Headings","On");
	else pMgr->setGlobalOption("Headings","Off");

    if(CSwordBibleDoc::redchrist_tags) pMgr->setGlobalOption("Words of Christ in Red","On");
	else pMgr->setGlobalOption("Words of Christ in Red","Off");


	return 0L;
}

void CBibleSheet::DisplayReference(CString text, CString title)
{
	 // Display the dialog box
		if (!pDlg)
        pDlg = new CScripRef;

	    if (!::IsWindow(pDlg->GetSafeHwnd()))
        pDlg->Create(IDD_DIALOG_SCRIPREF, NULL);

		 pDlg->ShowWindow(SW_SHOWNORMAL); 

		 pDlg->SetText(text);
		 pDlg->SetWindowTextA(title);
		 
}

void CBibleSheet::OnDestroy()
{
	if (pDlg)
    {
       if (::IsWindow(pDlg->GetSafeHwnd()))
            pDlg->EndDialog(IDCANCEL);
        delete pDlg;
			
    }
	pDlg = NULL;

	CPropertySheet::OnDestroy();

	
}


